<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close :before-close="closeDailog"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="空间名称" prop="spaceName">
                <el-input v-model="dataForm.spaceName" placeholder="空间名称"></el-input>
            </el-form-item>
            <el-form-item label="空间编号" prop="spaceCode">
                <el-input v-model="dataForm.spaceCode" placeholder="空间编号"></el-input>
            </el-form-item>
            <el-form-item label="空间路径" prop="spacePath">
                <el-input v-model="dataForm.spacePath" placeholder="空间路径"></el-input>
            </el-form-item>
            <el-form-item label="空间描述" prop="spaceDesc">
                <el-input v-model="dataForm.spaceDesc" placeholder="空间描述"></el-input>
            </el-form-item>
            <el-form-item label="所属集团" prop="pkGroup">
                <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup" placeholder="所属集团">
                </ec-group-tree-select>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <ec-org-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg"
                    placeholder="请选择所属组织"></ec-org-tree-select>
            </el-form-item>
            <el-form-item label="删除标识" prop="isDelete">
                <ec-dict-select v-model="dataForm.isDelete" placeholder="请选择删除标识" dictType="is_delete"></ec-dict-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as filespaceService from '@/api/file/filespace'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkFileSpace: 0,
                    spaceCode: '',
                    spaceName: '',
                    spaceDesc: '',
                    pkGroup: '',
                    pkOrg: '',
                    spacePath: '',
                    isDelete: '',
                    revision: '',
                },
                dataRule: {
                    spaceName: [{
                        required: true,
                        message: '空间名称不能为空',
                        trigger: 'blur'
                    }],
                    spaceCode: [{
                        required: false,
                        message: '空间编号不能为空',
                        trigger: 'blur'
                    }],
                    spaceDesc: [{
                        required: false,
                        message: '空间描述不能为空',
                        trigger: 'blur'
                    }],
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    spacePath: [{
                        required: true,
                        message: '空间路径不能为空',
                        trigger: 'blur'
                    }],
                    isDelete: [{
                        required: true,
                        message: '删除标识不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkFileSpace = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkFileSpace) {
                        filespaceService.getfilespaceinfo(this.dataForm.pkFileSpace).then(data => {
                            this.dataForm.spaceName = data.spaceName
                            this.dataForm.spaceCode = data.spaceCode
                            this.dataForm.spaceDesc = data.spaceDesc
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.spacePath = data.spacePath
                            this.dataForm.isDelete = data.isDelete
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkFileSpace': this.dataForm.pkFileSpace || undefined,
                            'spaceName': this.dataForm.spaceName,
                            'spaceCode': this.dataForm.spaceCode,
                            'spaceDesc': this.dataForm.spaceDesc,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'spacePath': this.dataForm.spacePath,
                            'isDelete': this.dataForm.isDelete,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkFileSpace ? filespaceService.addfilespace(params) :
                            filespaceService.updatefilespace(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>